<template>
    <div class="icon-body">
        <el-input v-model="searchText" class="icon-search" clearable placeholder="请输入图标名称">
            <template #suffix><i class="el-icon-search el-input__icon" /></template>
        </el-input>
        <div class="icon-list">
            <div class="list-container">
                <div v-for="icon in filteredIcons" :key="icon.font_class"
                    :class="['icon-item', { active: selectedIcon === icon.font_class }]" @click="selectIcon(icon)"
                    :title="icon.name">
                    <i :class="'iconfont icon-' + icon.font_class" style="font-size: 18px;"></i>
                    <span class="icon-name">{{ icon.name }}</span>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
export default {
    props: {
        modelValue: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            icons: [],
            searchText: '',
            selectedIcon: ''
        }
    },
    emits: ['update:modelValue'],
    computed: {
        filteredIcons() {
            return this.icons.filter(icon => {
                return icon.name.toLowerCase().includes(this.searchText.toLowerCase())
            })
        }
    },
    watch: {
        value: {
            immediate: true,
            handler(newVal) {
                this.selectedIcon = newVal
            }
        },
        selectedIcon(newVal) {
            this.$emit('update:modelValue', newVal)
        }
    },
    async created() {
        const response = await fetch('/iconfont/iconfont.json')
        const data = await response.json()
        this.icons = data.glyphs
    },
    methods: {
        selectIcon(icon) {
            this.selectedIcon = icon.font_class
            document.body.click()
        }
    }
}
</script>

<style lang='scss' scoped>
.icon-body {
    width: 100%;
    padding: 10px;

    .icon-search {
        position: relative;
        margin-bottom: 5px;
    }

    .icon-list {
        height: 200px;
        overflow: auto;

        .list-container {
            display: flex;
            flex-wrap: wrap;

            .icon-item {
                padding: 0 5px;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                padding: 10px;
                width: 10%;
                cursor: pointer;

                &:hover {
                    background: #ececec;
                    border-radius: 5px;
                }

                .icon {
                    flex-shrink: 0;
                }

                span {
                    display: inline-block;
                    vertical-align: -0.15em;
                    fill: currentColor;
                    padding-left: 2px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }
            }

            .icon-name {
                font-size: 12px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                width: 100%;
                text-align: center;
                color: #606266;
                margin-top: 5px;
            }

            .icon-item.active {
                background: #ececec;
                border-radius: 5px;

            }
        }
    }
}
</style>